package com.watson.minio.springminio.file.util;



import cn.hutool.core.util.ObjectUtil;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;


/**
 * @Description: 文件工具类
 * @Author: fan
 * @Data: 2021/5/8 10:20
 * @Version: 1.0v
 */
public class FileUtil {

    /**
     * 判断文件是否图片
     * @param file 文件
     * @return boolean
     */
    public static boolean getImg(File file) {
        try {
            Image image = ImageIO.read(file);
            if(!ObjectUtil.isEmpty(image)){
                return true;
            }
        }catch (Exception e){
            return false;
        }
        return false;
    }


    /**
     * 判断文件是否图片
     * @param multipartFile 文件
     * @return boolean
     */
    public static boolean getImg(MultipartFile multipartFile) {
        try {
            File file = multipartFileToFile(multipartFile);
            Image image = ImageIO.read(file);
            if(!ObjectUtil.isEmpty(image)){
                return true;
            }
        }catch (Exception e){
            return false;
        }
        return false;
    }

    /**
     * MultipartFile 转 File
     *
     * @param file
     * @throws Exception
     */
    public static File multipartFileToFile(MultipartFile file) throws Exception {
        File toFile;
        // 为空
        if (ObjectUtil.isEmpty(file) || file.getSize() <= 0) {
            return null;
        }

        InputStream ins = file.getInputStream();
        toFile = new File(file.getOriginalFilename());
        inputStreamToFile(ins, toFile);
        ins.close();

        return toFile;
    }

    /**
     * MultipartFile 转 File
     *
     * @param file
     * @throws Exception
     */
    public static File multipartFileToFile(MultipartFile file,String path) throws IOException {
        if(ObjectUtil.isEmpty(path)){
            throw new RuntimeException("文件路径不合理");
        }
        mkdirs(path);
        File toFile;
        // 为空
        if (ObjectUtil.isEmpty(file) || file.getSize() <= 0) {
            return null;
        }

        InputStream ins = file.getInputStream();
        toFile = new File(path+File.separator+file.getOriginalFilename());
        inputStreamToFile(ins, toFile);
        ins.close();

        return toFile;
    }

    /**
     * 当文件夹不存在时，mkdirs会自动创建多层目录，区别于mkdir．
     * (mkdir如果父目录不存在则会抛出异常)
     * @param destPath
     *            存放目录
     */
    public static void mkdirs(String destPath) {
        File file = new File(destPath);
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
    }

    /**
     * 获取流文件
     * @param ins
     * @param file
     */
    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String checkType(String type) {

        switch (type) {
            case "FFD8FF": return "jpg";
            case "89504E": return "png";
            case "474946": return "jif";

            default: return null;
        }
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    /**
     * 作者： wt
     * 时间  2020年6月4日10:19:16
     * 描述：随机数，数量自定义
     * @param o
     * @return
     */
    public static synchronized String getRandomNumber(Integer o) {
        Random random = new Random();
        StringBuffer buffer = new StringBuffer();
        Date date = new Date();
        String str = dateToFileNameString(date);
        buffer.append(str+"_");
        for (int i = 0; i < o; i++) {
            buffer.append(random.nextInt(9) % (9) + 1);
        }
        return buffer.toString();
    }

    public static String dateToFileNameString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH~mm~ss");
        return sdf.format(date);
    }
}
